package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectTypeInfoEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 *
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    /**
     * 枚举身份的识别
     */
    @Override
    public SubjectTypeInfoEnum getHandlerType() {
        return SubjectTypeInfoEnum.MULTIPLE;
    }

    /**
     * 多选题目的插入
     *
     * @param subjectInfoBo
     */
    @Override
    public void add(SubjectInfoBo subjectInfoBo) {

        //多选题目插入
        List<SubjectMultiple> subjectMultipleList=new LinkedList<>();
        subjectInfoBo.getOptionList().forEach(option -> {
            SubjectMultiple subjectMultiple= MultipleSubjectConverter.INSTANCE.convertBotoEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);

        });
        subjectMultipleService.batchInsert(subjectMultipleList);

    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple=new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result=subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBoList= MultipleSubjectConverter.INSTANCE.convertEntitytoBoList(result);
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBoList);
        return subjectOptionBO;
    }
}
